﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// Summary description for CarHelper
/// </summary>
public class CarHelper
{

    public static List<string> GetBrands()
    {
        List<string> result = new List<string>();

        foreach (KeyValuePair<string, List<string>> item in CarsDic)
        {
            result.Add(item.Key);
        }
        return result;
    }

    private static Dictionary<string, List<string>> carsDic;

    public static Dictionary<string, List<string>> CarsDic
    {
        get
        {
            if (carsDic == null)
            {
                carsDic = LoadCars();
            }
            return carsDic;
        }
    }


    private static Dictionary<string, List<string>> LoadCars()
    {
        Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
        XmlDocument carsDoc = new XmlDocument();
        string carsFile = HttpContext.Current.Server.MapPath("Brands.xml");
        carsDoc.Load(carsFile);
        foreach (XmlNode item in carsDoc.ChildNodes[0].ChildNodes)
        {
            string brand = item.Attributes["BrandName"].InnerText;
            string model = item.Attributes["ModelName"].InnerText;
            List<string> modelList;
            if (result.ContainsKey(brand))
            {
                modelList = result[brand];
                modelList.Add(model);
            }
            else
            {
                modelList = new List<string>();
                modelList.Add(model);
                result.Add(brand, modelList);
            }
        }
        return result;
    }

    public static List<string> GetModelsByBrand(string brandName)
    {
        if (CarsDic.ContainsKey(brandName))
        {
            return CarsDic[brandName];
        }
        else
        {
            return new List<string>();
        }

    }
}
